package com.zf.yichat.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

/**
 * 
 * @author wu
 */
@Slf4j
public class ResourceConfig {

    private static ResourceConfig instance;

    private static Properties properties = null;

    /** ADMIN公钥路径 */
    public static final String ADMIN_PUBLIC_KEY_PATH = System.getProperty("user.dir") + File.separator + "st_admin"
            + File.separator + "web" + File.separator + "src" + File.separator + "main" + File.separator + "resources"
            + File.separator + "key" + File.separator + "public.key";

    /** ADMIN私钥路径 */
    public static final String ADMIN_PRIVATE_KEY_PATH = System.getProperty("user.dir") + File.separator + "st_admin"
            + File.separator + "web" + File.separator + "src" + File.separator + "main" + File.separator + "resources"
            + File.separator + "key" + File.separator + "private.key";


    private ResourceConfig(String path) {
        properties = new Properties();
        FileInputStream msgIn = null;
        try {
            // 加载资源文件messages.properties
            msgIn = new FileInputStream(path);
            properties.load(msgIn);
        } catch (IOException ex) {
            log.error("加载 " + path + "资源文件失败", ex);
        } finally {
            if (msgIn != null) {
                try {
                    msgIn.close();
                } catch (IOException ex) {
                    log.error("关闭流失败", ex);
                }
            }
        }

    }

    public synchronized static ResourceConfig getInstance() {
        if (instance == null || properties == null) {
            instance = new ResourceConfig("");
        }
        return instance;
    }

    public String getPropertyTrim(String key) {
        String val = properties.getProperty(key);
        if (val != null) {
            return val.trim();
        }
        return null;
    }

    public String getPropertyTrim(String key, String defaultVal) {
        String val = properties.getProperty(key);
        if (StringUtils.isBlank(val)) {
            return defaultVal;
        }
        return val.trim();
    }

    public String getProperty(String key) {
        String value = null;
        value = properties.getProperty(key);
        return value;
    }

    public String getProperty(String key, String defaultVal) {
        String val = properties.getProperty(key);
        if (StringUtils.isBlank(val)) {
            return defaultVal;
        }
        return val;
    }
}
